//
//  DDBossJobsListViewController.m
//  DingDongZP
//
//  Created by Jyh on 16/6/7.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#import "DDBossJobsListViewController.h"
#import "DDJobsService.h"
#import "DDDiscoverJobCell.h"
#import "DDWorkDetailViewController.h"

@interface DDBossJobsListViewController ()

@property (strong, nonatomic) DDJobsService *jobService;

@end

@implementation DDBossJobsListViewController

#pragma mark
#pragma mark - View LifeCycle

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    if (self.jobDetail) {
        self.title = self.jobDetail.employername;
    }
    
    [self setupData];
    [self setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark
#pragma mark - Private Methods

- (void)setupData
{
    self.jobService = [[DDJobsService alloc] init];
    
    [self requestAllInterstingJobInfo];
}

- (void)setupView
{
    [self configureTableView];
}

- (void)configureTableView
{
    self.tableView.rowHeight = 182;
    self.tableView.tableFooterView = [[UIView alloc] init];
    
    UINib *cellNib = [UINib nibWithNibName:@"DDDiscoverJobCell" bundle:nil];
    [self.tableView registerNib:cellNib forCellReuseIdentifier:DDDiscoverJobCellIdentifier];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self addRefreshHeader];
    [self addRefreshFooter];
}

- (void)loadNewData
{
    [self.dataSource removeAllObjects];
    self.startIndexPage = 0;
    [self requestAllInterstingJobInfo];
}

- (void)loadMoreData
{
    [self requestAllInterstingJobInfo];
}

- (void)requestAllInterstingJobInfo
{
    WEAKSELF
    if (!self.jobDetail.userid) {
        [self showToastOnCenter:@"BossId有误"];
        return;
    }
    
    [self.jobService queryBossPublishedJobsWithBossID:self.jobDetail.userid page:[NSString stringWithFormat:@"%ld",self.startIndexPage] successBlock:^(id responseObject) {
        if ([weakSelf isValidateResponseData:responseObject]) {
            self.startIndexPage ++;
            // 请求数据成功
            DDJobsBaseModel *baseModel = [DDJobsBaseModel yy_modelWithJSON:responseObject[@"data"]];
            
            [weakSelf setFooterHidden:[baseModel.hasNextPage isEqualToString:@"0"]];
            
            [weakSelf.dataSource addObjectsFromArray:baseModel.jobs];
            
            if ([weakSelf.dataSource count] == 0) {
                [weakSelf showNoDataViewWithImage:nil text:STR_NODATA];
            } else {
                [weakSelf hideNoDataView];
            }
            
            weakSelf.tableView.hidden = self.dataSource.count == 0;
            
            if ([weakSelf isRefreshing]) {
                [weakSelf endRefreshHeader];
                [weakSelf endRefreshFooter];
            }
            
            [weakSelf.tableView reloadData];
            
        } else {
            [weakSelf responseUnValidate:responseObject];
        }
    } failBlock:^(id responseObject, NSError *error) {
        [weakSelf requestFaild];
    }];

}

#pragma mark
#pragma mark - CustomDelegate

#pragma mark -
#pragma mark UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DDDiscoverJobCell *cell = (DDDiscoverJobCell *)[tableView dequeueReusableCellWithIdentifier:DDDiscoverJobCellIdentifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if ([self.dataSource count] > indexPath.row) {
        [cell updateViewWithData:(DDJobsDetail *)self.dataSource[indexPath.row]];
    }
    
    return cell;
}

#pragma mark -
#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    DDWorkDetailViewController *workVC = [[DDWorkDetailViewController alloc] init];
    workVC.jobDetail = (DDJobsDetail *)self.dataSource[indexPath.row];
    
    workVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:workVC animated:YES];
}

@end
